package com.yi.mall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.yi.mall.api.CartFeign;
import com.yi.mall.api.SkuInfoFeign;
import com.yi.mall.common.model.Cart;
import com.yi.mall.common.model.SkuInfo;
import com.yi.mall.common.utils.Result;
import com.yi.mall.order.service.IOpenFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OpenFeignServiceImpl implements IOpenFeignService {
    @Qualifier("com.yi.mall.api.CartFeign")
    @Autowired
    private CartFeign cartFeign;
    @Qualifier("com.yi.mall.api.SkuInfoFeign")
    @Autowired
    private SkuInfoFeign skuInfoFeign;
    @Override
    public SkuInfo getSkuInfoById(Long id) {
        // 获取远程调用结果
        Result result = skuInfoFeign.getById(id);
        // 如果调用结果正常，获取数据
        if(result.getCode() == 2000 && result.getData() != null){
            SkuInfo skuInfo = JSON.parseObject(JSON.toJSONString(result.getData()), SkuInfo.class);
            return skuInfo;
        }
        return null;
    }

    @Override
    public List<SkuInfo> listSkuInfoByCondition(String cname, String bname) {
        // 获取远程调用结果
        Result result = skuInfoFeign.listByCondition(cname,bname);
        // 如果调用结果正常，获取数据
        if(result.getCode() == 2000 && result.getData() != null){
            List<SkuInfo> skuInfos = JSON.parseArray(JSON.toJSONString(result.getData()), SkuInfo.class);
            return skuInfos;
        }
        return null;
    }

    @Override
    public void updateSkuInfo(SkuInfo skuInfo) {
        // 远程调用
        skuInfoFeign.update(skuInfo);
    }
    @Override
    public void cartrm(List<String> ids) {
      cartFeign.rmall(ids);
    }
    @Override
    public void delete(String id) {
        cartFeign.delete(id);
    }

    @Override
    public Cart findByid(String id) {
        Object data = cartFeign.findByid(id).getData();
        Cart cart = JSON.parseObject(JSON.toJSONString(data), Cart.class);
        return cart;
    }
}
